package com.glacier.yuyuan.modules.admin.sys.web;

import cn.hutool.core.util.StrUtil;
import com.glacier.common.core.domain.Page;
import com.glacier.common.core.domain.Result;
import com.glacier.common.core.domain.form.ListWrapper;
import com.glacier.yuyuan.common.web.BaseController;
import com.glacier.yuyuan.modules.admin.sys.domain.DictData;
import com.glacier.yuyuan.modules.admin.sys.service.DictDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * date 2023-01-12 19:35
 * 字典数据控制层
 *
 * @author glacier
 * @version 1.0.0
 */
@RequestMapping("${yuyuan.adminPath}/sys/dictData")
@Controller
public class DictDataController extends BaseController {

    private DictDataService dictDataService;

    @Autowired
    public void setDictDataService(DictDataService dictDataService) {
        this.dictDataService = dictDataService;
    }

    @ResponseBody
    @PostMapping("/data")
    public Result<List<DictData>> data(DictData dictData) {
        List<DictData> list = dictDataService.findList(dictData);
        return Result.ok("ok", list, list.size());
    }

    @ResponseBody
    @PostMapping("/page")
    public Result<List<DictData>> page(Page<DictData> page, DictData dictData) {
        page = dictDataService.findPage(page, dictData);
        return Result.ok("ok", page.getData(), page.getTotal());
    }

    @GetMapping("/form")
    public String form(DictData dictData, Model model) {
        if (StrUtil.isNotBlank(dictData.getId())) {
            dictData = dictDataService.findById(dictData.getId());
        }
        model.addAttribute("entity", dictData);
        return "admin/sys/dict/dictDataForm";
    }

    @PostMapping("/save")
    @ResponseBody
    public Result<Integer> save(DictData dictData) {
        return Result.ok("操作成功！", dictDataService.save(dictData));
    }

    @ResponseBody
    @PostMapping("/del")
    public Result<String> del(ListWrapper<String> form) {
        dictDataService.batchDelete(form.getDatas());
        return Result.ok("操作成功", "");
    }
}
